package com.xzz.string;

import java.util.Stack;

/**
 * @author: hhz
 * @create: 2022-01-18 16:28
 * 856. 括号的分数
 **/
public class ScoreOfParentheses {
    public static void main(String[] args) {
        System.out.println(scoreOfParentheses("()"));
        System.out.println(scoreOfParentheses("(())"));
        System.out.println(scoreOfParentheses("()()"));
        System.out.println(scoreOfParentheses("(()(()))"));

    }

    public static int scoreOfParentheses(String s) {
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)=='('){
                stack.push(0);
            }else if(s.charAt(i)==')') {
                Integer p1 = stack.pop();
                Integer p2 = stack.pop();
                stack.push(Math.max(p1*2,1)+p2);
            }
        }
        return stack.pop();
    }
}
